import json

import scrapy

from spider_job.items import SpiderJobItem


class TencentSpider(scrapy.Spider):
    name = 'tencent'
    allowed_domains = ['tencent.com']
    start_urls = ['https://careers.tencent.com/tencentcareer/api/post/Query?' \
                  'keyword=&pageIndex=1&pageSize=10']

    def parse(self, response):
        body = response.body

        json_body = json.loads(body)

        for job in json_body["Data"]["Posts"]:
            itemdata = SpiderJobItem()
            itemdata["title"] = job.get("RecruitPostName")
            itemdata["company"] = "腾讯(tencent)"
            itemdata["salary"] = ''
            itemdata["address"] = job.get("LocationName")
            itemdata["post"] = job.get("Responsibility")
            itemdata["experience"] = ''

            # yield itemdata

            # 职位详情页URL
            # "PostURL": "http://careers.tencent.com/jobdesc.html?postId=1448132334552358912",
            post_id = job.get("PostId")
            post_detail_url = "https://careers.tencent.com/tencentcareer/api/post/ByPostId?postId=" + post_id
            yield scrapy.Request(url=post_detail_url, meta={"item": itemdata}, callback=self.parse_detail)

    def parse_detail(self, response):
        body = response.body
        json_body = json.loads(body)
        detail = json_body["Data"]

        itemdata = response.meta["item"]
        itemdata['experience'] = detail["Requirement"]
        itemdata['message'] = detail["Responsibility"]

        yield itemdata
